package com.jw.qme.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jw.qme.domain.dto.MessageGroupDTO;
import com.jw.qme.domain.po.Group;
import com.jw.qme.domain.po.GroupMember;
import com.jw.qme.domain.po.MessageGroup;
import com.jw.qme.domain.po.User;
import com.jw.qme.domain.vo.GroupVO;
import com.jw.qme.domain.vo.MessageGroupVO;
import com.jw.qme.mapper.GroupMemberMapper;
import com.jw.qme.mapper.MessageGroupMapper;
import com.jw.qme.service.GroupMemberService;
import com.jw.qme.service.MessageGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;

/**
 * ClassName: com.jw.qme.service.impl
 * Datetime: 2025/4/4 下午7:24
 * Description:
 *
 * @author 汪佳泉
 * @version 1.0
 * @since 1.0
 */
@Service
public class GroupMemberServiceImpl extends ServiceImpl<GroupMemberMapper, GroupMember>
        implements GroupMemberService {

    @Autowired
    private GroupMemberMapper groupMemberMapper;

    @Autowired
    private MessageGroupService messageGroupService;

    @Override
    public List<GroupVO> getGroupsByUsername(String username) {
        List<Group> groups = groupMemberMapper.getGroupsByUsername(username);
        List<GroupVO> groupVOS = BeanUtil.copyToList(groups, GroupVO.class);
        for (GroupVO groupVO: groupVOS) {
            // 为每一个 group 填充对应的群聊消息
            List<MessageGroupVO> messageGroups = messageGroupService.listByGroupId(new MessageGroupDTO(groupVO.getGroupId(), LocalDateTime.now(), 20));
            groupVO.setGroupMessages(messageGroups);
        }
        return groupVOS;
    }

    @Override
    public List<User> getGroupMembers(String groupId) {
        return groupMemberMapper.getUsersByGroupId(groupId);
    }
}
